package com.example.demo;

import com.openhtmltopdf.DOMBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;
import java.net.URL;

public class Test {


    public static void main(String[] args)   {
        generatePDF("C:\\Users\\Administrator\\Desktop\\风险评估报告\\企业风险报告.html","D:test.pdf");
    }

    public static void generatePDF(String inputHtmlPath, String outputPdfPath)
    {
        try (OutputStream os = new FileOutputStream(outputPdfPath)) {
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.withW3cDocument(html5ParseDocument(inputHtmlPath),new File("C:\\Users\\Administrator\\Desktop\\风险评估报告").toURI().toString());
            builder.toStream(os);
            builder.run();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  static org.w3c.dom.Document html5ParseDocument(String urlStr) throws IOException
    {

        Document parse = Jsoup.parse(new File(urlStr), "UTF-8");

        return DOMBuilder.jsoup2DOM(parse);
    }
}
